<?php
/**
 * Index控制器
 *
 * @author		黑冰 <954650386@qq.com>
 */

use \waf\Waf;
use \waf\log\WLog;
use \waf\controller\WController;

class IndexController extends WController
{
    /**
     * monitor count
     */
    public function indexAction() 
    {
        $server_name	= trim($this->getRequest()->getParam('get',  array())['server_name']);
        $module_name	= trim($this->getRequest()->getParam('get',  array())['module_name']);
        $interface_name	= trim($this->getRequest()->getParam('get',  array())['interface_name']);
        $date_time		= trim($this->getRequest()->getParam('get',  array())['date_time']);

        WLog::logInfo("server_name=$server_name, module_name=$module_name, interface_name=$interface_name, date_time=$date_time", __FILE__, __LINE__, __FUNCTION__);

        if(empty($date_time))   $date_time = date('Ymd');

        if($server_name && $module_name && $interface_name)
        {
            $ret = MMonitor::get_server_module_list($server_name, $module_name, Waf::getConfig('monitor', 'count_log_path'));
            if($ret)
            {
                $this->_data['module_list']    = $ret;

                $ret = MMonitor::getMonitorCountData($server_name, $module_name, $interface_name, $date_time);
                $this->_data['monitor_count']   = $ret;
            }else{
                $this->_code    = 10003;
            }
        }

        // 获取所有interface list
        else if($server_name && $module_name)
        {
            $ret = MMonitor::get_server_module_list($server_name, $module_name, Waf::getConfig('monitor', 'count_log_path'));
            if($ret)
            {
                $this->_data['module_list']    = $ret;
            }else{
                $this->_code    = 10002;
            }
        }

        // 获取所有module list
        else if($server_name)
        {
            $ret = MMonitor::get_server_module_list($server_name, '', Waf::getConfig('monitor', 'count_log_path'));
            if($ret)
            {
                $this->_data['module_list']    = $ret;
            }else{
                $this->_code    = 10001;
            }
        }

        // param error
        else{
            $this->_code    = 10000;
        }

        WLog::logDebug("output = ".WLog::toJson($this->_data), __FILE__, __LINE__, __FUNCTION__);
        $this->output();
    }

    /**
     * monitor interface
     */
    public function interfaceAction()
    {
        $server_name	= trim($this->getRequest()->getParam('get',  array())['server_name']);
        $module_name	= trim($this->getRequest()->getParam('get',  array())['module_name']);
        $interface_name	= trim($this->getRequest()->getParam('get',  array())['interface_name']);
        $date_time		= trim($this->getRequest()->getParam('get',  array())['date_time']);

        WLog::logInfo("server_name=$server_name, module_name=$module_name, interface_name=$interface_name, date_time=$date_time", __FILE__, __LINE__, __FUNCTION__);

        if(empty($date_time))   $date_time = date('Ymd');

        if($server_name && $module_name && $interface_name)
        {
            $ret = MMonitor::get_server_module_list($server_name, $module_name, Waf::getConfig('monitor', 'interface_log_path'));
            if($ret)
            {
                $this->_data['module_list']    = $ret;

                $ret = MMonitor::getMonitorInterfaceData($server_name, $module_name, $interface_name, $date_time);
                $this->_data['monitor_interface']   = $ret;
            }else{
                $this->_code    = 10003;
            }
        }

        // 获取所有interface list
        else if($server_name && $module_name)
        {
            $ret = MMonitor::get_server_module_list($server_name, $module_name, Waf::getConfig('monitor', 'interface_log_path'));
            if($ret)
            {
                $this->_data['module_list']    = $ret;
            }else{
                $this->_code    = 10002;
            }
        }

        // 获取所有module list
        else if($server_name)
        {
            $ret = MMonitor::get_server_module_list($server_name, '', Waf::getConfig('monitor', 'interface_log_path'));
            if($ret)
            {
                $this->_data['module_list']    = $ret;
            }else{
                $this->_code    = 10001;
            }
        }

        // param error
        else{
            $this->_code    = 10000;
        }

        WLog::logDebug("output = ".WLog::toJson($this->_data), __FILE__, __LINE__, __FUNCTION__);
        $this->output();
    }
}